package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    //注入远程调用openfei接口
    @Autowired
    private DictFeignClient dictFeignClient;

    //根据id查询登录用户下的就诊人信息
    @Override
    public List<Patient> findAllPatient(Long userId) {

        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patients = patientMapper.selectList(queryWrapper);

        //查询到数据中的市等编号
        patients.stream().forEach(item ->{
            this.packPatient(item);
        });

        return patients;
    }

    @Override
    public void savePatient(Patient patient) {
        patientMapper.insert(patient);
    }

    //根据id查询就诊人信息
    @Override
    public Patient findByIdPatient(Long id) {

        Patient patient = patientMapper.selectById(id);
        this.packPatient(patient);

        return patient;
    }

    @Override
    public void updatePatient(Patient patient) {
        patientMapper.updateById(patient);
    }

    @Override
    public void deletePatient(Long id) {
        patientMapper.deleteById(id);
    }

    @Override
    public List<Patient> findByUserIdPatient(Long userId) {

        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patients = patientMapper.selectList(queryWrapper);
        patients.stream().forEach(item->{
            this.packPatient(item);
        });
        return patients;
    }

    //Patient对象里面其他参数封装
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        // String contactsCertificatesTypeString =dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        // patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }


}
